﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;
using Orchard.Localization;
using Orchard.UI.Notify;
using Rework.SocialMetadata.Services;

namespace Rework.SocialMetadata.Settings {
    public class SocialMetadataSettingsEvent : ContentDefinitionEditorEventsBase {
        private readonly ISocialMetadataServices _socialMetadataServices;
        private readonly INotifier _notifier;
        
        public SocialMetadataSettingsEvent(
            ISocialMetadataServices socialMetadataServices,
            INotifier notifier) {

            _socialMetadataServices = socialMetadataServices;
            _notifier = notifier;
        }

        public Localizer T { get; set; }

        #region Content Part
        public override IEnumerable<TemplateViewModel> PartEditor(ContentPartDefinition definition) {
            if (definition.Name != "SocialMetadataPart")
                yield break;

            var model = definition.Settings.GetModel<SocialMetadataPartSettings>();
            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> PartEditorUpdate(ContentPartDefinitionBuilder builder, IUpdateModel updateModel) {
            if (builder.Name != "SocialMetadataPart")
                yield break;

            var model = new SocialMetadataPartSettings();
            updateModel.TryUpdateModel(model, "SocialMetadataPartSettings", null, null);
            builder.WithSetting("SocialMetadataPartSettings.PatternDefault", !string.IsNullOrWhiteSpace(model.PatternDefault) ? model.PatternDefault : null);
            yield return DefinitionTemplate(model);
        }
        #endregion

        #region Content Type
        public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition) {
            if (definition.PartDefinition.Name != "SocialMetadataPart")
                yield break;

            var model = definition.Settings.GetModel<SocialMetadataTypePartSettings>();

            if (String.IsNullOrWhiteSpace(model.Pattern)) {
                // Retrieve from the Content Part then
                var partModel = definition.PartDefinition.Settings.GetModel<SocialMetadataPartSettings>();
                model.Pattern = partModel.PatternDefault;
            }
            
            yield return DefinitionTemplate(model);
        }

        public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel) {
            if (builder.Name != "SocialMetadataPart")
                yield break;

            var model = new SocialMetadataTypePartSettings();
            updateModel.TryUpdateModel(model, "SocialMetadataTypePartSettings", null, null);
            builder.WithSetting("SocialMetadataTypePartSettings.Pattern", !string.IsNullOrWhiteSpace(model.Pattern) ? model.Pattern : null);
            yield return DefinitionTemplate(model);
        }
        #endregion

    }
}